﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sales;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Sales
{
    public partial class frmMaintainProductPlanApplicationQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private SalesProdPlanApplicationEntity mSelectedProdPlanApplication;
        public SalesProdPlanApplicationEntity SelectedProdPlanApplication
        {
            get { return mSelectedProdPlanApplication; }
        }
        public List<SalesProdPlanApplicationEntity> selectedProdPlanAppList
        {
            get;
            set;
        }
        private bool isMulti=false;
        #endregion

        #region Constructor
        public frmMaintainProductPlanApplicationQuery()
        {
            InitializeComponent();
        }
        public frmMaintainProductPlanApplicationQuery(bool multi)
        {
            InitializeComponent();
            gridColumn10.Visible = true;
            isMulti = multi;
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            cmbStatus.SetSelectedValue(NormalStatus.STATUS03.ToString());

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colAPPLICATION_TYPE, "AT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colPRODUCT_TYPE_ID, "PTYPE", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtSalesOrder.Tag != null && txtSalesOrder.Tag.GetType() == typeof(SalesOrderEntity))
            {
                SalesOrderEntity order = (SalesOrderEntity)txtSalesOrder.Tag;
                condition += " AND PPA.ORDER_ID = '" + order.OrderId + "' ";
            }
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                MstCustomerEntity customer = (MstCustomerEntity)txtCustomer.Tag;
                condition += " AND PPA.CUSTOMER_ID = '" + customer.CustomerId + "' ";
            }
            if (string.IsNullOrEmpty(txtApplicationCode.Text) == false)
            {
                condition += " AND PPA.APPLICATION_CODE LIKE '%" + txtApplicationCode.Text + "%'";
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                condition += " AND PPA.APPLICATION_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpApplicationDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpApplicationDateTo.EditValue!=null)
            {
                condition += " AND PPA.APPLICATION_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpApplicationDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (txtApplicationPerson.Tag != null && txtApplicationPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtApplicationPerson.Tag;
                condition += " AND PPA.APPLICATION_PERSON = '" + entity.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND PPA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "APPLICATION_CODE DESC";
            DataTable dt = SalesProdPlanApplicationBs.GetByCondition(condition, orderBy, "");
            dt.Columns.Add("IS_SELECTED", typeof(Boolean));
            foreach (DataRow dr in dt.Rows)
            {
                dr["IS_SELECTED"] = false;
            }
            gvMain.DataSource = dt;
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (isMulti == false)
            {
                if (mSelectedProdPlanApplication == null)
                {
                    MessageBox.Show("请选择一个生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                DataTable dt = gvMain.DataSource as DataTable;
                selectedProdPlanAppList = new List<SalesProdPlanApplicationEntity>();
                SalesProdPlanApplicationEntity entity;
                foreach (DataRow dr in dt.Rows)
                {
                    if (Convert.ToBoolean(dr["IS_SELECTED"]) == true)
                    {
                        entity = new SalesProdPlanApplicationEntity(dr);
                        selectedProdPlanAppList.Add(entity);
                    }
                }
                if (selectedProdPlanAppList == null || selectedProdPlanAppList.Count == 0)
                {
                    MessageBox.Show("请至少选择一个生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
        }

        private void txtSalesOrder_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainSalesOrderQuery frm = new frmMaintainSalesOrderQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedSalesOrder != null)
            {
                txtSalesOrder.Text = frm.SelectedSalesOrder.OrderCode;
                txtSalesOrder.Tag = frm.SelectedSalesOrder;
            }
            else
            {
                txtSalesOrder.Text = "";
                txtSalesOrder.Tag = null;
            }
        }

        private void txtApplicationPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtApplicationPerson.Text = frm.SelectedPerson.NameCn;
                txtApplicationPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }
        }

        private void txtCustomer_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainCustomerQuery frm = new frmMaintainCustomerQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedCustomer != null)
            {
                txtCustomer.Text = frm.SelectedCustomer.CustomerName;
                txtCustomer.Tag = frm.SelectedCustomer;
            }
            else
            {
                txtCustomer.Text = "";
                txtCustomer.Tag = null;
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedProdPlanApplication = new SalesProdPlanApplicationEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedProdPlanApplication == null)
            {
                MessageBox.Show("请选择一个生产计划申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.DialogResult = DialogResult.OK;
        }

        #endregion

    }
}
